/*
 * \brief  PCI bus access
 * \author Christian Helmuth
 * \date   2008-10-01
 *
 * The DDE Kit provides exactly one virtual PCI bus.
 */

/*
 * Copyright (C) 2008-2010 Genode Labs GmbH
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__DDE_KIT__PCI_H_
#define _INCLUDE__DDE_KIT__PCI_H_

#include <dde_kit/types.h>

#define dde_kit_pci_dev pci_node_t
struct dde_kit_pci_dev;


/********************************
 ** Configuration space access **
 ********************************/

/**
 * Read byte from PCI config space
 *
 * \param slot  slot number
 * \param func  function number
 * \param pos   offset in config space
 *
 * \retval val  read value
 *
 * \return 0    success, -1 otherwise
 */
int dde_kit_pci_readb(int slot, int func, int pos, dde_kit_uint8_t  *val);

/**
 * Read word from PCI config space
 *
 * \param slot  slot number
 * \param func  function number
 * \param pos   offset in config space
 *
 * \retval val  read value
 *
 * \return 0    success, -1 otherwise
 */
int dde_kit_pci_readw(int slot, int func, int pos, dde_kit_uint16_t *val);

/**
 * Read dword from PCI config space
 *
 * \param slot  slot number
 * \param func  function number
 * \param pos   offset in config space
 *
 * \retval val  read value
 *
 * \return 0    success, -1 otherwise
 */
int dde_kit_pci_readl(int slot, int func, int pos, dde_kit_uint32_t *val);

/**
 * Write byte to PCI config space
 *
 * \param slot  slot numer
 * \param func  function  number
 * \param pos   offset in config space
 * \param val   value to write
 *
 * \return 0    success, -1 otherwise
 */
int dde_kit_pci_writeb(int slot, int func, int pos, dde_kit_uint8_t val);

/**
 * Write word to PCI config space
 *
 * \param slot  slot numer
 * \param func  function  number
 * \param pos   offset in config space
 * \param val   value to write
 *
 * \return 0    success, -1 otherwise
 */
int dde_kit_pci_writew(int slot, int func, int pos, dde_kit_uint16_t  val);

/**
 * Write dword to PCI config space.
 *
 * \param slot  slot numer
 * \param func  function  number
 * \param pos   offset in config space
 * \param val   value to write
 *
 * \return 0    success, -1 otherwise
 */
int dde_kit_pci_writel(int slot, int func, int pos, dde_kit_uint32_t  val);


/***************************
 ** Convenience functions **
 ***************************/

/**
 * Find a PCI device
 *
 * \param slot   slot ID
 * \param func   function ID
 *
 * \return next PCI device or 0
 */
struct dde_kit_pci_dev *dde_kit_pci_find_devfn(int slot, int func);

/**
 * Get next PCI device
 *
 * \param start  device, where search starts
 *
 * \return next PCI device or 0
 */
struct dde_kit_pci_dev *dde_kit_pci_next_device(struct dde_kit_pci_dev *start);

/**
 * Get device-function descriptor
 *
 * \param dev  device handle
 *
 * \return combined device-function ID
 */
unsigned char dde_kit_pci_get_devfn(struct dde_kit_pci_dev *dev);

/**
 * Initialize PCI subsystem
 */
void dde_kit_pci_init(void);


#endif /* _INCLUDE__DDE_KIT__PCI_H_ */
